<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>

      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :immediate-check="false"
        @load="onLoad"
      >
        <article-item
          v-for="obj in searchResultList"
          :key="obj.art_id"
          :obj="obj"
          :isshow="false"
          @click.native="listdetail(obj.art_id)"
        ></article-item>
      </van-list>

  </div>
</template>

<script>
import { searchReaultAPI } from '@/api'
import ArticleItem from '@/views/Home/components/ArticleItem.vue'
export default {
  components: { ArticleItem },
  name: 'SearchResult',
  data () {
    return {
      page: 1,
      searchResultList: [],
      loading: false,
      finished: false,
      refreshing: false
    }
  },
  async created () {
    this.getsearchListFn()
  },
  methods: {
    async getsearchListFn () {
      const q = this.$route.params.keywords
      const res = await searchReaultAPI({ page: this.page, q })
      if (res.data.data.results === null || res.data.data.results.length === 0) {
        this.finished = true
      }
      this.searchResultList = [...this.searchResultList, ...res.data.data.results]
      this.loading = false
    },
    async onLoad () {
      this.page++
      this.getsearchListFn()
    },
    listdetail (article_id) {
      this.$router.push(`/article_detail?aid=${article_id}`)
    }
  }

}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
